package com.itheima.project_cloth.controller;

import com.itheima.project_cloth.pojo.Cloth;
import com.itheima.project_cloth.pojo.PageBean;
import com.itheima.project_cloth.pojo.Result;
import com.itheima.project_cloth.service.ClothService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cloths")
public class ClothController {
    @Autowired
    private ClothService clothService;

    @GetMapping
    public Result selectClothByName(String name){

        Cloth cloth = clothService.selectClothByName(name);

        return Result.success(cloth);
    }

    @PostMapping
    public Result updateClothById(@RequestBody Cloth cloth){
        clothService.updateClothById(cloth);

        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteClothById(@PathVariable List<Integer> ids){
        clothService.deleteClothById(ids);

        return Result.success();
    }

    @PostMapping("/insert")
    public Result InsertCloth(@RequestBody Cloth cloth) {
        log.info("新增的衣服为:{}", cloth);
        clothService.Insert(cloth);
        return Result.success();
    }

    @GetMapping("/{kindId}")
    public Result SelectClothByKind_Id(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer pageMax,@PathVariable Integer kindId) {
        log.info("要筛选的种类Id为{}", kindId);
        PageBean pageBean = clothService.selectClothByKind_Id(page, pageMax, kindId);
        return Result.success(pageBean);
    }
}
